<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Home\Controller;
use Common\Controller\CommonController;
use Home\Logic\PayLogic;
use Org\Util\String;
/**
 * Description of RobOrderController
 *
 * @author Administrator
 */
class RobOrderController extends HomeController {

    public function _initialize() {

        parent::_initialize();
    }


    /**
     * 立即购买添加购物车
     */
     public function buyNow() {
        $open_id = I('openid');
        if($open_id){
        $goods_id = I('goods_id');
        $goods= M('Goods')->where(array('goods_id' => $goods_id, 'is_sale' => 1, 'is_limit' => 1))->find();
        if((int)$goods['end_time']-(int)time()<=0){
            $this->ajaxReturn(['status' => -1, 'msg' => '拼抢已结束']);
        }
        $price_attr_id = I('price_attr_id');
        $other_attr = I('other_attr');
        $num = 1;
        //现在拼抢的数量
        $daq['is_pay']=0;
        $daq['open_id']=$open_id;
           $over=M('rob_orders')->join('left join yang_rob_goods_orders on yang_rob_goods_orders.order_id = yang_rob_orders.order_id')
                    ->where(array('goods_id' => $goods_id, 'open_id'=>$open_id))->count();
           $c=M('config');
           $chu=$c->select();
            foreach($chu as $k=>$v){
                if($v['key']=='MAXNUM'){
                    $num1=$v['value'];
                }
            }
        if($over>=$num1){
            $msg="最多只能拼抢".$num1.'个';
            $this->ajaxReturn(['status' => -2, 'msg' => $msg]);
        }
        $cart_find = M('Rob_cart')->where("is_check=1")->delete();
        $cart = [
            'open_id' => $open_id,
            'goods_id' => $goods_id,
            'goods_attr_id' => $price_attr_id,
            'attr_names' => $other_attr,
            'is_check' => 1
        ];
            $cart['num'] = $num;
            $r = M('Rob_cart')->add($cart);
      
        if (!$r) {
            $this->ajaxReturn(['status' => -1, 'msg' => '服务器繁忙']);
        }
        $this->ajaxReturn(['status' => 1, 'msg' => '请求成功']);
        }else{
               $this->ajaxReturn(['status' => 0, 'msg' => 'openid呢!']);
        }
    }
//    查看拼抢订单
    public function orderlook()
    {
        // $open_id=I('openid');
         $goods_id=I('goods_id'); //如果提交商品id就根据商品id查询
         $open_id=I('openid');
         //$goods_attr_id=I('goods_attr_id'); 
        //        查询默认地址
            if($open_id){
                 $datas=M('user_address')->where(array('open_id'=>$open_id,'isDefault'=>1))->find();
                if($datas){
                $data['address']=$datas;
                }else{
                    $data['address']=0;
                }
       
        // if($open_id){
            if($goods_id!=''&&$goods_attr_id!=''){
                $da['goods_id']=$goods_id;
                $da['open_id']=$open_id;
               /* $data['goods_attr_id']=I('goods_attr_id');*/ 
            }else{
                $da['open_id']=$open_id;
            }
            // dump($da);die();
            // $this->ajaxReturn(['status' => $da, 'msg' => '请登录']);
    //查询购物车
            $data['cart']=M('rob_cart')->where($da)->select();
            if($data['cart']){ //查商品
                foreach ($data['cart'] as &$v){
                $v['goods']=M('goods')->where(array('goods_id'=>$v['goods_id']))->find();
                if($v['goods']['end_time']>time()){
                	$v['goods']['end_time']=$v['goods']['end_time'];
                	$v['goods']['start_time']=time();
                }else{
                	$v['goods']['end_time']=0;
                	$v['goods']['start_time']=0;
                }
                //$v['attr']=M('goods_attr')->where(array('goods_attr_id'=>$v['goods_attr_id']))->field('price')->find();
                }
                //dump($data);die;
               $price=0;
               foreach($data['cart'] as $k =>$v){
                    $price=$v['goods']['goods_price'];
               }
            $data['peice']=$price;
            //echo $price;die;
                $this->ajaxReturn(['status' => 1,'data'=>$data, 'msg' => '成功']);
            }else{
                $this->ajaxReturn(['status' => 0, 'msg' => '购物车没有数据']);
            }  
                
        }else{
             $this->ajaxReturn(['status' => 0, 'msg' => '请登录']);
        }
//        dump($data);
    }


    /**
     * 立即购买生成订单
     */
    public function buyNowOrders() {
        $open_id = I('openid');
        $address = M('User_address')->where(array('open_id' => $open_id, 'isDefault' => 1))->find();
           if (empty($address)) {
                $this->ajaxReturn(['status' => 0, 'msg' => '请先添加地址']);
           }
        if($open_id){
        $content=I('content');
        $cart = M('rob_cart')->where(array('open_id' => $open_id, 'is_check' => 1))->select();
        foreach ($cart as $c) {
            $goods_id = $c['goods_id'];
            $num = intval($c['num']);
            $other_attr = $c['attr_names'];
            $price_attr_id = $c['goods_attr_id'];
            $order_no = getOrderNo1($open_id);
            while (M('rob_orders')->where(array('order_no' => $order_no))->find()) {
                $order_no = getOrderNo1($open_id);
            }
            $order_status = -1;
            //$send_money = $this->config['send_money'];
            // 
            $goods_attr = M('Goods_attr')->where(array('goods_attr_id' => $price_attr_id))->find();
            $goods = M('Goods')->where(array('goods_id' => $goods_id, 'is_limit' => 1))->find();
            if($goods['end_time']<time()){
            	$start_time='0';
            	$end_time='0';
            }else{
            	$start_time=time();
            	$end_time=$goods['end_time'];
            }
            $order_goods = [
                'order_id' => 0,
                'goods_id' => $goods_id,
                'num' => $num,
                'price' => $goods['goods_price'],
                'attr_id' => $price_attr_id,
                'attr_name' => $other_attr,
                'goods_name' => $goods['name'],
                'image' => $goods['image'],
                'status' => 0,
                'main' => 1,
                'cart_id' => $c,
                'start_time'=>$start_time,
                'end_time'=>$end_time,
            ];
            $totle_money = $num * $order_goods['price'];
           /*  if (!empty($goods_attr['price']) && $goods_attr['price'] != 0) {
                
            } */
            $fan=M('Goods_attr')->where(array('goods_attr_id' => $price_attr_id))->find();
            M('Goods_attr')->where(array('goods_attr_id' => $price_attr_id))->setDec('attr_kucun', $num);
            //$arr_goods[] = $order_goods;
            $r = M('rob_goods_orders')->add($order_goods);
            if (!$r) {
                $this->ajaxReturn(['status' => 0, 'msg' => '请求失败']);
            }
            $send_money = 0;
           /*  if ($send_money == 9999) {
                $send_money_s = 0;
            } else {
                $send_money_s = $send_money;
            } */

           /*  $totle_money += $send_money_s; */
            $is_pay = 0;

           
            $username = $address['username'];
            $user_address = $address['areaid_1'] . $address['areaid_2'] . $address['areaid_3'] . $address['address'];
            $phone = $address['phone'];
            $add_time = time();
            $orders = [
                'order_no' => $order_no,
                'order_status' => $order_status,
                'total_money' => $totle_money,
                'send_money' => 0,
                'pay_type' => 0,
                'is_pay' => $is_pay,
                'open_id' => $open_id,
                'username' => $username,
                'user_address' => $user_address,
                'phone' => $phone,
                'add_time' => $add_time,
                'is_pingjia' => 0,
                'start_time'=>$start_time,
                'end_time'=>$end_time,
            ];
            //插入订单表
            $order_add = M('Rob_orders')->add($orders);
            //加入邀请语
            $yao['content']=$content;
            $yao['add_time']=time();
            $yao['rob_order']=$order_add;
            $use=M('yaoq')->add($yao);
            //处理插入的order_goods表中的数据 添加 order_id字段属性
            $order = M('rob_goods_orders')->where(array('id' => $r))->setField('order_id', $order_add);
            if(!$order_add){
                $this->ajaxReturn(['status' => 0, 'msg' => '请求失败!']);
            }

            $delete_cart=M('Rob_cart')->where(array('cart_id' => $c))->delete();
        }
                   /*  $rr['id']='';
                    $rr['open_id']=$open_id;
                    $rr['sz_type']=1;
                    $rr['content']='发起拼抢';
                    $rr['addtime']=time();
                    $rr['money']=1;
                    $rr['type']=2;
                    $f=M('finance')->add($rr); */
        //查询新生成的订单
            // $ordersqe=M('rob_orders')->where(array('order_id'=>$order_add))->field('')->find();
        $ip = get_client_ip();
        //调用支付
        
        //$this->getParameters();
        
        $start_time=(string)$start_time;
        $end_time=(string)$end_time;
        //$this->ajaxReturn(['status' => 1, 'msg' => '成功生成订单','start_time'=>$start_time,'end_time'=>$end_time, 'totle_money' => $totle_money, 'order_id' => $order_add, 'order_no' => $order_no, 'ip' => $ip]);
	        $pay = new PayLogic();
	        $pay->pay($order_add,$open_id,-1, $totle_money,1,$start_time,$end_time);//付款
        }else{
             $this->ajaxReturn(['status' => 0,'data'=>$open_id,'msg' => '网络异常!!!!!!']);
        }
    }

 
    
    /**
     * 删除购物车
     */
    public function delCart() {
        $open_id = session('openid');
        if($open_id){
        $cart_id = I('cart_id');
        $r = M('Rob_cart')->where(array('cart_id' => $cart_id, 'open_id' => $open_id))->delete();
        if (!$r) {
            $this->ajaxReturn(['status' => -1, 'msg' => '服务器繁忙']);
        }
        $this->ajaxReturn(['status' => 1, 'msg' => '请求成功']);
        }else{
               $this->ajaxReturn(['status' => 0, 'msg' => 'openid呢!']);
        }
    }
    
    
    
     /**
     * 拼抢成功
     */
    public function orderSuccess(){
        $open_id =I('openid');
        if($open_id){


    //    $open_id = session();
        $order = M('rob_orders')->where(array('order_status'=>-1,'open_id'=>$open_id))
                ->join('left join yang_rob_goods_orders on yang_rob_goods_orders.order_id = yang_rob_orders.order_id')
                ->find();
        $this->ajaxReturn($order);
        }else{
               $this->ajaxReturn(['status' => 0, 'msg' => 'openid呢!']);
        }
    }
    
    /**
     * 我的拼抢 _新
     */
    public function myOrder(){
    	$open_id = I('openid');
    	if($open_id){
    		//goodsId
    		$ordergoods = M('rob_goods_orders g')->field("a.username,a.order_status,g.goods_name,a.is_pay,a.order_id,a.order_no,g.price,a.add_time,a.order_id,g.image,a.pay_money,g.order_id,a.open_id,g.goods_id,a.zhulirenshu")->join('left join yang_rob_orders a on g.order_id=a.order_id ')->where(array('open_id'=>$open_id))->select();
    		//dump($ordergoods);
    		//循环此订单
    		
    		foreach($ordergoods as $kk=>$vv){
    			$g=M('goods')->field('goods_id,start_time,end_time')->where(array('goods_id'=>$vv['goods_id']))->find();
    			if($g['end_time']<=time()){
    				$ordergoods[$kk]['start_time']=0;
    				$ordergoods[$kk]['end_time']=0;
    			}
    			if($g['end_time']>time()){
    				$ordergoods[$kk]['end_time']=$g['end_time'];
    				$ordergoods[$kk]['start_time']=time();
    			}
    			if($ordergoods[$kk]['end_time']==0 && $ordergoods[$kk]['start_time']==0){
    				$ordergoods[$kk]['order_status']=-6;
    			}
    			$gorder = M('rob_goods_orders g')->join('left join yang_rob_orders a on g.order_id=a.order_id ')->where(array('goods_id'=>$vv['goods_id']))->select();
    			//查询此商品的所有订单
    			$num=1;
    			//dump($gorder);
    			//dump($vv['zhulirenshu']);
    			foreach($gorder as $k=>$v){
   				//dump($v['zhulirenshu']);
    				if($v['zhulirenshu']>$vv['zhulirenshu']){
    					$num++;
    				}elseif($v['zhulirenshu']==$vv['zhulirenshu']){
    					if($v['add_time']<$vv['add_time']){
    						$num++;
    					}
    				}
    			}
    			//dump($num);
    			$ordergoods[$kk]['mingci']=$num;
    		    
    		}
    		$ordergoods = $this->my_sort($ordergoods,'end_time',SORT_DESC,SORT_STRING);
    		$this->ajaxReturn($ordergoods);
    	}else{
    		$this->ajaxReturn(['status' => 0, 'msg' => '网络错误!']);
    	}
    }
    /**
     * 我的拼抢  
     */
    public function myOrder_(){
        $open_id = I('openid');
        if($open_id){
           $order = M('rob_goods_orders g')->join('left join yang_rob_orders a on g.order_id=a.order_id ')->where(array('open_id'=>$open_id))->select();
           $order = $this->my_sort($order,'zhulirenshu',SORT_DESC,SORT_STRING);
           foreach($order as $k=>$v){
           	$g=M('goods')->field('goods_id,start_time,end_time')->where(array('goods_id'=>$v['goods_id']))->find();
	           	if($g['end_time']<=time()){
	           		$order[$k]['start_time']=0;
	           		$order[$k]['end_time']=0;
	           	}
	           	if($g['end_time']>time()){
	           		$order[$k]['end_time']=$g['end_time'];
	           		$order[$k]['start_time']=time();
	           	}
            	$order[$k]['mingci']=$k+1;
            	
            	$order[$k]['status']=$v['order_status'];
            }
            //dump($order);die;
            $this->ajaxReturn($order);
        }else{
               $this->ajaxReturn(['status' => 0, 'msg' => '网络错误!']);
        }
    }
    //排序
    public function my_sort($arrays,$sort_key,$sort_order=SORT_ASC,$sort_type=SORT_NUMERIC ){
    	if(is_array($arrays)){
    		foreach ($arrays as $array){
    			if(is_array($array)){
    				$key_arrays[] = $array[$sort_key];
    			}else{
    				return false;
    			}
    		}
    	}else{
    		return false;
    	}
    	array_multisort($key_arrays,$sort_order,$sort_type,$arrays);
    	return $arrays;
    }
    /**
     * 拼抢情况
     */
    public function orderSituation(){
    	$order_id = I('order_id');
    	$open_id=I('openid');
    	if($open_id){
    		//查询订单和商品订单信息
    		$dingdan=M('rob_goods_orders')->join('left join yang_rob_orders on yang_rob_orders.order_id = yang_rob_goods_orders.order_id')
    		->where(array('yang_rob_goods_orders.order_id'=>$order_id))->find();
    		$goods_id=$dingdan['goods_id'];
    		$zhulirenshu=$dingdan['zhulirenshu'];
    		foreach($dingdan as $kk=>$vv){
    			$g=M('goods')->field('goods_id,start_time,end_time')->where(array('goods_id'=>$dingdan['goods_id']))->find();
    			if($g['end_time']<time()){
    				$dingdan['start_time']=0;
    				$dingdan['end_time']=0;
    				$dingdan['zhuangtai']=1;
    			}
    			if($g['end_time']>=time()){
    				$dingdan['end_time']=$g['end_time'];
    				$dingdan['start_time']=time();
    				$dingdan['zhuangtai']=0;
    			}
    		}
            $start_time=$dingdan['start_time'];
            $end_time=$dingdan['end_time'];
            $add_time=$dingdan['add_time'];
    		$order['order'] = $dingdan;
    		 
    		//查询帮助记录
    		$orderwq=M('rob_log l')
    		->field('m.headimg,m.nickname,l.created_at')
    		->join('yang_member m ON m.openid=l.f_open_id')
    		->where(array('rob_order_id'=>$order_id))->select();
    		if($orderwq){
    			$order['log']= $orderwq;
    		}else{
    			$order['log']=0;
    		}
    		//$order['log_count']=count($order['log']);
    		$order['log_count']= $dingdan['zhulirenshu'];
    		//排名计算
    		$gorder = M('rob_goods_orders g')->join('left join yang_rob_orders a on g.order_id=a.order_id ')->where(array('goods_id'=>$goods_id))->order('zhulirenshu desc')->select();
    		//查询此商品的所有订单
    		$num=1;
    		//dump($gorder);
    		//dump($vv['zhulirenshu']);
    		$zlr=0;
    		foreach($gorder as $k=>$v){
    			//dump($v['zhulirenshu']);
    			if($v['zhulirenshu']>$zhulirenshu){
    				$num++;
    				$zlr=$k;
    			}elseif($v['zhulirenshu']==$zhulirenshu){
    				if($v['add_time']<$add_time){
    					$num++;
    					$zlr=$k;
    				}
    			}
    		}
    		$order['dijiming']=$num;
    		$order['cjr']=$gorder[$zlr]['zhulirenshu']-$gorder[$zlr+1]['zhulirenshu']+1;
    		if($order['dijiming']==1){
    			$order['cjr']=0;
    		}
    		/* $minc=0;
    		$shanm=0;
    		$wod=0;
    		$wo_key=[];
    		$pai=M('rob_orders')->field('order_id,pai,zhulirenshu')->order('zhulirenshu desc,start_time asc')->select();
    		//查询所有订单
    		foreach ($pai as $k=>$v){
    			$wo_key[]=$k;
    			if($v['order_id']==$order_id){
    				//按助力人数排名
    				$minc=$k;//第几名
    				$wod=$pai[$k];//我的排名
    				$shanm=$pai[$k-1];
    			}
    		}
    		$order['ddd']=$wo_key;
    		$order['d1dd']=$wod['zhulirenshu'];
    		$order['dijiming']=$minc+1;//0开始
    		if($order['dijiming']=='1'){
    			$order['cjr']=0;
    		}elseif($shanm['zhulirenshu']==$wod['zhulirenshu']){
    			$order['cjr']=$shanm['zhulirenshu']-$wod['zhulirenshu']+1;
    		}else{
    			$order['cjr']=$shanm['zhulirenshu']-$wod['zhulirenshu'];
    		}
    		  */
    		$order['user']=M('member')->where(array('openid'=>$open_id))->find();
    		//dump($order);die;
    		$this->ajaxReturn($order);
    	}else{
    		$this->ajaxReturn(['status' => 0, 'msg' => 'openid呢!']);
    	}
    }
     /**
     * 拼抢情况
     */
    public function orderSituation_xin(){
        $order_id = I('order_id');
        $open_id=I('openid');
        if($open_id){
        //查询订单和商品订单信息
        $dingdan=M('rob_goods_orders')->join('left join yang_rob_orders on yang_rob_orders.order_id = yang_rob_goods_orders.order_id')
                ->where(array('yang_rob_goods_orders.order_id'=>$order_id))->find();
        //dump($dingdan);
          foreach($dingdan as $kk=>$vv){
        	$g=M('goods')->field('goods_id,start_time,end_time')->where(array('goods_id'=>$dingdan['goods_id']))->find();
        	if($g['end_time']<time()){
        		$dingdan['start_time']=0;
        		$dingdan['end_time']=0;
        	}
        	if($g['end_time']>=time()){
        		$dingdan['end_time']=$g['end_time'];
        		$dingdan['start_time']=time();
        	}
        } 
        
        $order['order'] = $dingdan;
       
        //查询帮助记录
        $orderwq=M('rob_log l')
        ->field('m.headimg,m.nickname,l.created_at')
        ->join('yang_member m ON m.openid=l.f_open_id')
        ->where(array('rob_order_id'=>$order_id))->select();
        if($orderwq){
             $order['log']= $orderwq;
        }else{
             $order['log']=0;
        }
        $order['log_count']= $dingdan['zhulirenshu'];
         //排名计算
        $minc=0;
        $shanm=0;
        $wod=0;
        $wo_key=[];
        $pai=M('rob_orders')->field('order_id,pai,zhulirenshu')->order('zhulirenshu desc,start_time asc')->select();
        //查询所有订单
        foreach ($pai as $k=>$v){
        	$wo_key[]=$k;
            if($v['order_id']==$order_id){
            	//按助力人数排名
                $minc=$k;//第几名
                $wod=$pai[$k];//我的排名
                $shanm=$pai[$k-1];
            }
        }
        $order['ddd']=$wo_key;
        $order['d1dd']=$wod['zhulirenshu'];
        $order['dijiming']=$minc+1;//0开始
        if($order['dijiming']=='1'){
        	$order['cjr']=0;
        }elseif($shanm['zhulirenshu']==$wod['zhulirenshu']){
        	$order['cjr']=$shanm['zhulirenshu']-$wod['zhulirenshu']+1;
        }else{
        	$order['cjr']=$shanm['zhulirenshu']-$wod['zhulirenshu'];
        } 
       
        $order['user']=M('member')->where(array('openid'=>$open_id))->find();
       
        $this->ajaxReturn($order);
        }else{
               $this->ajaxReturn(['status' => 0, 'msg' => 'openid呢!']);
        }
    }
    /**
     * 拼抢邀请
     */
    public function orderInvite(){
        $order_id = I('order_id');
        $open_id = I('openid');
        if($open_id){
        //查询订单和商品订单信息
        $order['order'] = M('rob_orders')
                ->join('left join yang_rob_goods_orders on yang_rob_orders.order_id = yang_rob_goods_orders.order_id')
                ->where(array('yang_rob_orders.order_id'=>$order_id))->find();
        //查询帮助记录
        $goodsinfo=M('goods')->where(array('goods_id'=>$order['order']['goods_id']))->find();
        if(time()>$goodsinfo['end_time']){
        	$order['order']['start_time']='0';
        	$order['order']['end_time']='0';
        }else{
        	$order['order']['start_time']=(string)time();
        	$order['order']['end_time']=(string)$goodsinfo['end_time'];
        }
        //dump($order);
       // $order['log']=->select();
        $order['nickname'] = M('member')->where(array('openid'=>$order['order']['open_id']))->field('nickname,headimg')->find();

       $dq['rob_order_id']=$order_id;
        $order['ren']=M('rob_log')->where($dq)->count();

 //添加名次
        $minc=M('rob_orders')->where('order_id='.$order_id)->setInc('pai');
        //订单拼抢内容
        $pq=M('yaoq')->where("rob_order='{$order_id}'")->find();
        //$goodsinfo=M('goods')->where(array('goods_id'=>$order['order']['goods_id']))->find();
        $order['rule'] = $goodsinfo['pqnews'];
        $order['jinqian'] = $goodsinfo['zhuli_price'];
        $order['info']=$pq;
        $this->ajaxReturn($order);
        }else{
               $this->ajaxReturn(['status' => 0, 'msg' => 'openid呢!']);
        }
    }
    /*  邀请支付接口  */
    
 	public function yaoqingzhifu(){
    	$order_id=I('order_id');
    	$open_id=I('openid');
    	if(!$open_id){
    		$this->ajaxReturn(['msg'=>'缺少参数','status'=>'-1']);
    	}else{
    		$or=M('rob_orders');
    		$olist=$or->where(array('order_id'=>$order_id))->find();
    		$goods=M('rob_goods_orders')->where(array('order_id'=>$order_id))->find();
    		$gg=M('goods')->where(array("goods_id"=>$goods['goods_id']))->find();
    		if(time()>$gg['end_time']){
    			$this->ajaxReturn(['msg'=>'拼抢结束','status'=>'-3']);
    		}
    		if($olist['open_id']==$open_id){
    			//首先是自己的订单
    			$log=M('rob_log')->where(array('f_open_id'=>$open_id,'rob_order_id'=>$order_id))->find();
    			//此订单自己是不是已经助力过
    			if($log){
    				$this->ajaxReturn(['msg'=>'只能助力一次','status'=>'-2']);
    			}else{
    				$og=M('rob_goods_orders')->where(array('order_id'=>$order_id))->find();
    				$goods=M('goods')->where(array('goods_id'=>$og['goods_id']))->find();
    				//dump($goods);
    				//echo $goods['zhuli_price'];die;
    				$pay = new PayLogic();
    				//$pay->pay($order_add,$open_id,-1, $totle_money,1);//付款
    				//echo  $goods['zhuli_price'];die;
    				$money=$goods['zhuli_price'];
    				$pay->pay($order_id,$open_id, '1',$money,'0');//付款
    			}
    		}else{
    			//别人的订单
    			$log=M('rob_log')->where(array('f_open_id'=>$open_id,'rob_order_id'=>$order_id))->find();
    			if($log){
    				$this->ajaxReturn(['msg'=>'只能助力一次','status'=>'-2']);
    			}else{
    				$og=M('rob_goods_orders')->where(array('order_id'=>$order_id))->find();
    				$goods=M('goods')->where(array('goods_id'=>$og['goods_id']))->find();
    				//dump($goods);
    				//echo $goods['zhuli_price'];die;
    				$pay = new PayLogic();
    				//$pay->pay($order_add,$open_id,-1, $totle_money,1);//付款
    				//echo  $goods['zhuli_price'];die;
    				$money=$goods['zhuli_price'];
    				$pay->pay($order_id,$open_id, '1',$money,'0');//付款
    		   }
    		}
    	}	
    }
    
       //分享功能
       public function fenx()
        {
            $order_id=I('order_id');
            $open_id=I('open_id');
            if($open_id){
                ///查询订单是否存在
                $order=M('rob_orders')->where(array('order_id'=>$order_id))->find();
                if($order){
                    /* $url='http://'.$_SERVER['HTTP_HOST'].'Home/RobOrder/orderInvite/order_id/'.$order_id; */
                    //查询发起邀请语
                    $yao=M('yaoq')->where(array('rob_order'=>$order_id))->find();
                    $detail=M('rob_goods_orders')->where(array('order_id'=>$order_id))->find();
                    $content=$yao['content'];
                    $img=$detail['image'];
                    /* $this->ajaxReturn(['status' => 1,'url'=>$url,'content'=>$content,'img'=>$img, 'msg' => '获取成功']); */
                }else{
                    $this->ajaxReturn(['status' => -3, 'msg' => '订单不存在']);
                }
            }else{
                $this->ajaxReturn(['status' => -2, 'msg' => '重新登陆没有openid']);
            }

        }
        /**
         * 获取jssdk需要用到的数据
         * @return array jssdk需要用到的数据
         */
        
/*         
        
     
        
        public function notify(){
        	// 获取xml
        	$xml=file_get_contents('php://input', 'r');
        	// 转成php数组
        	$data=$this->toArray($xml);
        	// 保存原sign
        	$data_sign=
        	$data['sign'];
        	// sign不参与签名
        	unset($data['sign']);
        	$sign=$this->makeSign($data);
        	// 判断签名是否正确  判断支付状态
        	if ($sign===$data_sign && $data['return_code']=='SUCCESS' && $data['result_code']=='SUCCESS') {
        		$result=$data;
        	}else{
        		$result=false;
        	}
        	// 返回状态给微信服务器
        	if ($result) {
        		$str='<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
        	}else{
        		$str='<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[签名失败]]></return_msg></xml>';
        	}
        	echo $str;
        	if ($result) {
        		$result1 = json_decode(decode($result['attach']),true);
        		if(empty($result1['open_id'])){
	        		$orde=M('rob_orders');
	        		$olist=$order->where(array('order_id'=>$result1['order_id']))->find();
	        		if($olist){
	        			$ginfo=M('rob_goods_orders');
	        			$gg=$ginfo->where(array('order_id'=>$ginfo['order_id']))->find();
	        			$jiage=$gg['price']*$gg['num'];
	        		    $orr['is_pay']=1;
	        		    $orr['pay_money']=$jiage;
	        		    $orr['total_money']=$jiage;
	        		    $orr['pay_time']=time();
	        		    $orr['order_status']=1;
	        		    $olist->where(array('order_id'=>$result1['order_id']))->save($oarr);
	        		    $user=M('member')->where(array('openid'=>$olist['open_id']))->find();
	        		    $ulist['jifen']=$user['jifen']+1;
	        		    $re=M('member')->where(array('openid'=>$olist['open_id']))->save($ulist);
	        		    $frr['id']='';
	        		    $frr['open_id']=$olist['open_id'];
	        		    $frr['sz_type']=1;
	        		    $frr['content']='发起拼抢';
	        		    $frr['add_time']=time();
	        		    $frr['type']=1;
	        		    $frr['money']=1;
	        		    $ff=M('finance')->add($frr);
	        		}
        		}else{
        			$orde=M('rob_orders');
        			$olist=$order->where(array('order_id'=>$result1['order_id']))->find();
        			if($olist){
	        			$arr['zhulirenshu']=$olist['zhulirenshu']+1;
	        			$olist->where(array('order_id'=>$result1['order_id']))->save($arr);
	        			$user=M('member')->where(array('openid'=>$olist['open_id']))->find();
	        			$ulist['jifen']=$user['jifen']+10;
	        			$re=M('member')->where(array('openid'=>$result1['open_id']))->save($ulist);
	        			$frr['id']='';
	        			$frr['open_id']=$result1['open_id'];
	        			$frr['sz_type']=1;
	        			$frr['content']='助力拼抢';
	        			$frr['add_time']=time();
	        			$frr['type']=1;
	        			$frr['money']=10;
	        			$ff=M('finance')->add($frr);
	        			//助力拼抢的记录
	        			$ginfo=M('rob_goods_orders');
	        			$gg=$ginfo->where(array('order_id'=>$ginfo['order_id']))->find();
	        			$log=M('rob_log');
	        			$logarr['id']='';
	        			$logarr['open_id']=$olist['open_id'];
	        			$logarr['f_open_id']=$result1['open_id'];
	        			$logarr['goods_orders_id']=$olist['order_id'];
	        			$goods=M('goods')->where(array('goods_id'=>$gg['goods_id']))->find();
	        			$logarr['f_price']=$goods['zhuli_price'];
	        			$logarr['created_at']=time();
	        			$logarr['rob_order_id']=$olist['order_id'];
	        			$log->add($logarr);
        			}
        		}
        	}
        }
       
                   // 生成签名
        // @return 签名，本函数不覆盖sign成员变量，如要设置签名需要调用SetSign方法赋值
         
        private function makeSign($data){
        	// 去空
        	$data=array_filter($data);
        	//签名步骤一：按字典序排序参数
        	ksort($data);
        	$string_a=http_build_query($data);
        	$string_a=urldecode($string_a);
        	//签名步骤二：在string后加入KEY
        	$string_sign_temp=$string_a."&key=".'YIf848GHJrighke7836rH783jsjgl818';
        	//签名步骤三：MD5加密
        	$sign = md5($string_sign_temp);
        	// 签名步骤四：所有字符转为大写
        	$result=strtoupper($sign);
        	return $result;
        } */

        
    
  
    
}
